<?php

namespace App\Http\Model;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class Mate extends Base
{
    protected $mateUserTable = 'mate_user';
    protected $loveTalkTable = 'mate_love_talk';
    protected $LoveMottoTable = 'mate_love_motto';

    
    //用户每日总匹配次数
    public $mateUserDayNum = 20;
    //缘分配对次数兑换需要钻石
    public $mateNeedDiamondNum = 10;
    //语音通话每分钟消耗钻石数
    public $voiceCallNeedNum = 30;
    //用户每日已使用匹配次数
    protected $mateUserDayUseNum = 'MATE:USERDAYUSENUM:';//string MATE:USERDAYUSENUM:{Ymd}:{user_id} => {int}
    //用户每日已购买次数
    protected $mateUserDayBuyNum = 'MATE:USERDAYBUYNUM:';//string MATE:USERDAYBUYNUM:{Ymd}:{user_id} => {int}
    //甜蜜情话
    protected $mateLoveTalkKey = 'MATE:LOVETALK';//string MATE:LOVETALK => {string}
    //爱情箴言
    protected $mateLoveMottoKey = 'MATE:LOVEMOTTO';//string MATE:LOVEMOTTO => {string}
    //可匹配主播
    protected $mateLoveUserKey = 'MATE:LOVEUSER';//string MATE:LOVEUSER => {string}

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    /**
     * 用户每日已购买次数
     */
    public function getMateUserDayBuyNum($userId){
        $key = $this->mateUserDayBuyNum.date("Ymd").':'.$userId;
        $num = (int) Redis::get($key);

        return $num;
    }

    /**
     * 增加用户每日已购买次数
     */
    public function incrMateUserDayBuyNum($userId, $num){
        $key = $this->mateUserDayBuyNum.date("Ymd").':'.$userId;
        Redis::INCRBY($key, $num);
        Redis::EXPIRE($key, 86400);

        return 1;
    }

    /**
     * 用户每日已使用匹配次数
     */
    public function getMateUserDayUseNum($userId){
        $key = $this->mateUserDayUseNum.date("Ymd").':'.$userId;
        $num = (int) Redis::get($key);

        return $num;
    }

    /**
     * 增加用户每日已使用匹配次数
     */
    public function incrMateUserDayUseNum($userId, $num){
        $key = $this->mateUserDayUseNum.date("Ymd").':'.$userId;
        Redis::INCRBY($key, $num);
        Redis::EXPIRE($key, 86400);

        return 1;
    }

    /**
     * 甜蜜情话
     */
    public function getLoveTalk(){
        $key = $this->mateLoveTalkKey;
        $list = Redis::get($key);
        if(empty($list)){
            $list = DB::table($this->loveTalkTable)
                ->select("id", "content")
                ->get()->toArray();
            $list = self::objToArray($list);
            Redis::setex($key, 3600, json_encode($list));
        }else{
            $list = json_decode($list, true);
        }
        if(!empty($list)) {
            $list = array_column($list, null, "id");
        }

        return $list;
    }

    /**
     * 爱情箴言
     */
    public function getLoveMotto(){
        $key = $this->mateLoveMottoKey;
        $list = Redis::get($key);
        if(empty($list)){
            $list = DB::table($this->LoveMottoTable)
                ->select("id", "content")
                ->get()->toArray();
            $list = self::objToArray($list);
            Redis::setex($key, 3600, json_encode($list));
        }else{
            $list = json_decode($list, true);
        }
        if(!empty($list)) {
            $list = array_column($list, null, "id");
        }

        return $list;
    }

    /**
     * 可匹配主播
     */
    public function getLoveUser(){
        $key = $this->mateLoveUserKey;
        $list = Redis::get($key);
        if(empty($list)){
            $list = DB::table($this->mateUserTable)
                ->select("id", "user_id", "voice")
                ->get()->toArray();
            $list = self::objToArray($list);
            Redis::setex($key, 3600, json_encode($list));
        }else{
            $list = json_decode($list, true);
        }
        if(!empty($list)) {
            $list = array_column($list, null, "id");
        }

        return $list;
    }


}
